package com.zzxz.practicaltrainingsys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Homeworkfinish;
import com.zzxz.practicaltrainingsys.mapper.HomeworkfinishMapper;
import com.zzxz.practicaltrainingsys.service.HomeworkfinishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HomeworkfinishServiceImpl implements HomeworkfinishService {
    @Autowired
    private HomeworkfinishMapper homeworkfinishMapper;

    @Override
    public List<Homeworkfinish> queryAllHomeworkfinish() {
        return homeworkfinishMapper.queryAllHomeworkfinish();
    }

    @Override
    public List<Homeworkfinish> getProHomeworkfinishById(Integer id) {
        return homeworkfinishMapper.getProHomeworkfinishById(id);
    }

    @Override
    public List<Homeworkfinish> getHomeHomeworkfinishById(Integer id) {
        return homeworkfinishMapper.getHomeHomeworkfinishById(id);
    }

    @Override
    public Homeworkfinish getDetailHomeworkfinishById(Integer id) {
        return homeworkfinishMapper.getDetailHomeworkfinishById(id);
    }

    @Override
    public List<Map<String, Object>> getAllHomeworkfinish() {
        return homeworkfinishMapper.getAllHomeworkfinish();
    }

    @Override
    public PageInfo<Map<String, Object>> findHomeworkfinishPages(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> lists = homeworkfinishMapper.getAllHomeworkfinish();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public List<Homeworkfinish> getHomeworksByStudentId(Integer studentId) {
        return homeworkfinishMapper.getHomeworksByStudentId(studentId);
    }

    @Override
    public Homeworkfinish getStudentHomeworkDetail(Integer studentId, Integer homeworkId) {
        return homeworkfinishMapper.getStudentHomeworkDetail(studentId, homeworkId);
    }

    @Override
    public int insertHomeworkfinish(Homeworkfinish homeworkfinish) {
        return homeworkfinishMapper.insertHomeworkfinish(homeworkfinish);
    }

    @Override
    public int updateHomeworkfinish(Homeworkfinish homeworkfinish) {
        homeworkfinish.setHomework_back(0);
        // 添加日志输出，便于调试
        System.out.println("更新作业提交记录: ID=" + homeworkfinish.getFinish_id() +
                ", homework_back=" + homeworkfinish.getHomework_back());

        int result = homeworkfinishMapper.updateHomeworkfinish(homeworkfinish);

        System.out.println("更新结果: " + result);
        return homeworkfinishMapper.updateHomeworkfinish(homeworkfinish);
    }

    @Override
    public List<Homeworkfinish> getTeamHomeworkDetails(Integer teamId, Integer homeworkId) {
        return homeworkfinishMapper.getTeamHomeworkDetails(teamId, homeworkId);
    }

    @Override
    public boolean isTeamLeader(Integer studentId, Integer teamId) {
        return homeworkfinishMapper.isTeamLeader(studentId, teamId) > 0;
    }

    @Override
    public Map<String, Object> getStudentTeamInfo(Integer studentId, Integer projectId) {
        // 这里需要根据你的数据库结构实现查询逻辑
        // 示例SQL查询，需要根据实际情况调整
        return homeworkfinishMapper.getStudentTeamInfo(studentId, projectId);
    }

    @Override
    public int gradeIndividualHomework(Integer homeworkId, Integer studentId, String homeworkMarks, String teacherComment) {
        try {
            System.out.println("Service: 批改个人作业，作业ID: " + homeworkId + ", 学生ID: " + studentId);
            return homeworkfinishMapper.gradeIndividualHomework(homeworkId, studentId, homeworkMarks, teacherComment);
        } catch (Exception e) {
            System.err.println("Service: 批改个人作业异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public int gradeTeamHomework(Integer homeworkId, Integer teamId, String homeworkMarks, String teacherComment) {
        try {
            System.out.println("Service: 批改小组作业，作业ID: " + homeworkId + ", 小组ID: " + teamId);
            return homeworkfinishMapper.gradeTeamHomework(homeworkId, teamId, homeworkMarks, teacherComment);
        } catch (Exception e) {
            System.err.println("Service: 批改小组作业异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public int returnIndividualHomework(Integer homeworkId, Integer studentId, Integer homeworkBack) {
        try {
            System.out.println("Service: 打回个人作业，作业ID: " + homeworkId + ", 学生ID: " + studentId);
            return homeworkfinishMapper.returnIndividualHomework(homeworkId, studentId, homeworkBack);
        } catch (Exception e) {
            System.err.println("Service: 打回个人作业异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public int returnTeamHomework(Integer homeworkId, Integer teamId, Integer homeworkBack) {
        try {
            System.out.println("Service: 打回小组作业，作业ID: " + homeworkId + ", 小组ID: " + teamId);
            return homeworkfinishMapper.returnTeamHomework(homeworkId, teamId, homeworkBack);
        } catch (Exception e) {
            System.err.println("Service: 打回小组作业异常: " + e.getMessage());
            throw e;
        }
    }
}
